# -*- coding: utf-8 -*-

import os
import sys
import time
from AzEasy.EasyJinja2 import TEasyJinja2
from AzEasy.EasyJson import TEasyJson
import datetime

# 全局变量
current_datetime = datetime.datetime.now()
cur_time = current_datetime.strftime('%Y-%m-%d %H:%M:%S')
cur_date = current_datetime.strftime('%Y/%m/%d')
cur_tool_version = "gen_hq_file.py >> V20231119"

# cur_time = time.localtime()
# cur_date = time.strftime("%Y/%m/%d", cur_time)
# cur_time = time.strftime("%Y/%m/%d", cur_time)


def GeneratorHopechartFile_CFile(file_name:str, encoding = "utf-8"):
    # cur_time = time.localtime()
    # cur_date = time.strftime("%Y/%m/%d", cur_time)

    dir_path = os.path.dirname(os.path.realpath(__file__)).replace('\\', '/')
    print(dir_path)

    ja = TEasyJinja2(dir_path + "/hopechart_template")
    user_dict = TEasyJson(dir_path + "/hopechart_template/hq_cfile_cfg.json").data
    user_dict["var_date"] = cur_date
    user_dict["var_filename"] = file_name
    user_dict["var_time"] = cur_time
    user_dict["var_tool_version"] = cur_tool_version
    ja.Render(user_dict, "hq_head.h.tpl", file_name + ".h")
    ja.Render(user_dict, "hq_source.c.tpl", file_name + ".c")


def GeneratorFile_Bat(file_name:str, encoding = "utf-8"):
    # cur_time = time.localtime()
    # cur_date = time.strftime("%Y/%m/%d", cur_time)

    dir_path = os.path.dirname(os.path.realpath(__file__)).replace('\\', '/')
    print(dir_path)

    ja = TEasyJinja2(dir_path + "/az_file_template")
    user_dict = TEasyJson(dir_path + "/az_file_template/az_file_cfg.json").data
    user_dict["var_date"] = cur_date
    user_dict["var_filename"] = file_name
    user_dict["var_time"] = cur_time
    user_dict["var_tool_version"] = cur_tool_version
    ja.Render(user_dict, "az_bat.bat.tpl", file_name + ".bat")

def GeneratorFile_Python(file_name:str, encoding = "utf-8"):
    # cur_time = time.localtime()
    # cur_date = time.strftime("%Y/%m/%d", cur_time)

    dir_path = os.path.dirname(os.path.realpath(__file__)).replace('\\', '/')
    print(dir_path)

    ja = TEasyJinja2(dir_path + "/az_file_template")
    user_dict = TEasyJson(dir_path + "/az_file_template/az_file_cfg.json").data
    user_dict["var_date"] = cur_date
    user_dict["var_filename"] = file_name
    user_dict["var_time"] = cur_time
    user_dict["var_tool_version"] = cur_tool_version
    ja.Render(user_dict, "az_py.py.tpl", file_name + ".py")


def GeneratorFile_Sh(file_name:str, encoding = "utf-8"):
    dir_path = os.path.dirname(os.path.realpath(__file__)).replace('\\', '/')
    print(dir_path)

    ja = TEasyJinja2(dir_path + "/az_file_template")
    user_dict = TEasyJson(dir_path + "/az_file_template/az_file_cfg.json").data
    user_dict["var_date"] = cur_date
    user_dict["var_filename"] = file_name
    user_dict["var_time"] = cur_time
    user_dict["var_tool_version"] = cur_tool_version
    ja.Render(user_dict, "az_sh.sh.tpl", file_name + ".sh", newline='\n')


if __name__ == "__main__":

    argv = sys.argv
    argc = len(argv)
    
    

    if len(argv) < 2:
        print("params error")
    elif argv[1] == '-c':
        file_name_lst = argv[2:]
        for file_name in file_name_lst:
            GeneratorHopechartFile_CFile(file_name, encoding = "utf-8")
    elif argv[1] == '-bat':
        file_name_lst = argv[2:]
        for file_name in file_name_lst:
            GeneratorFile_Bat(file_name, encoding = "utf-8")
    elif argv[1] == '-py':
        file_name_lst = argv[2:]
        for file_name in file_name_lst:
            GeneratorFile_Python(file_name, encoding = "utf-8")
    elif argv[1] == '-sh':
        file_name_lst = argv[2:]
        for file_name in file_name_lst:
            GeneratorFile_Sh(file_name, encoding = "utf-8")
    else:
        print("===== test demo =====")
        GeneratorHopechartFile_CFile("test_file")
        exit(1)



